
#include "app_device.h"



/*****************************************************                       
*				app_init_Device	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void app_init_Device( p_AppDevice p_Device,
					 DEVICE_RECV      p_Recv,
					 DEVICE_SEND_CMOT p_Create,
					 DEVICE_SEND_TMOT p_Timerout,
					 DEVICE_SEND_FUC  p_Send ){

	p_Device->d_Recv         = p_Recv;
	p_Device->d_CreateTimer  = p_Create;
	p_Device->d_DealTimerout = p_Timerout;
	p_Device->d_SendFuc      = p_Send;
}

/*****************************************************                       
*				app_Set_DeviceDelayFuc	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void app_Set_DeviceDelayFuc( p_AppDevice p_Device,
                            DEVICE_SEND_DELDYFUC d_SendDelayFuc ){
    p_Device->d_SendDelayFuc = d_SendDelayFuc;                            
}


/*****************************************************                       
*				app_set_Device_RecvBuffer	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void app_set_Device_RecvBuffer( p_AppDevice p_Device,
							   unsigned char *p_Data,
							   unsigned char  u_max,
							    uint16_t  u_per ){

	bsp_init_cir_queue( &p_Device->c_RecvBuffer,
		                 p_Data,u_max,u_per );
}

/*****************************************************                       
*				app_set_device_SendBuffer	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void app_set_device_SendBuffer( p_AppDevice p_Device,
								unsigned char *p_Data,
								unsigned char  u_max,
								uint16_t  u_per ){

	bsp_init_cir_queue( &p_Device->c_SendBuffer,
		                p_Data,u_max,u_per );
}

/*****************************************************                       
*				app_get_Device_RecvData
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
uint16_t app_get_Device_RecvData( p_AppDevice p_Device,
								  unsigned char *p_Data ,
								  uint16_t *p_len ,
                                  unsigned char *p_st ) {


	return bsp_pop_cir_queue_data( &p_Device->c_RecvBuffer,p_Data,p_len,p_st );
}

/*****************************************************                       
*				app_get_Device_SendData
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
uint16_t app_get_Device_SendData( p_AppDevice p_Device, unsigned char *p_Data ,
								 uint16_t *p_len,
                                  unsigned char *p_st ){

	return bsp_pop_cir_queue_data( &p_Device->c_SendBuffer,p_Data,p_len,p_st );

}



/*****************************************************                       
*				app_set_Device_RecvEvent	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void app_set_Device_RecvEvent( p_AppDevice p_Device, OS_EVENT *_event ){

	p_Device->p_OS_RecvEvent = _event;

}


/*****************************************************                       
*				app_set_Device_SendEvent	
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void app_set_Device_SendEvent( p_AppDevice p_Device, OS_EVENT *_event ){

	p_Device->p_OS_SendEvent = _event;
}

/*****************************************************                       
*				bsp_trigger_device_recv
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
uint16_t app_trigger_Device_RecvDeal( p_AppDevice p_Device, unsigned char *p_Data, 
									 uint16_t u_len,
                                     unsigned char u_st){
	
	uint16_t u_Ret = 0;

	u_Ret = bsp_push_cir_queue_data_tail( &p_Device->c_RecvBuffer,p_Data,u_len,u_st );
#if BSP_OS_EN == 1

	if( u_Ret ){

		OSQPost( p_Device->p_OS_RecvEvent,p_Device );
	}
#endif

	return u_Ret;
}


/*****************************************************                       
*				app_trigger_Device_SendDeal
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
uint16_t app_trigger_Device_SendDeal( p_AppDevice p_Device, unsigned char *p_Data, 
									 uint16_t u_len,
                                     unsigned char u_st ){
	uint16_t u_Ret = 0;
	
	u_Ret = bsp_push_cir_queue_data_tail( &p_Device->c_SendBuffer,p_Data,u_len,u_st );
#if BSP_OS_EN == 1
	
	if( u_Ret ){
		
		OSQPost( p_Device->p_OS_SendEvent,p_Device );
	}
#endif
	
	return u_Ret;
}


/*****************************************************                       
*				app_Device_RecvHandle
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
uint16_t app_Device_RecvHandle( p_AppDevice p_Device ){

	unsigned char recvData[DEVICE_RECV_MAX];
    uint16_t u_RecvLen = 0;
    unsigned char u_st = 0;
	bsp_timer t_timer ;

	memset( &t_timer,0,sizeof(bsp_timer));
	if( p_Device == NULL ){

		return FALSE;
	}
	
	if( bsp_pop_cir_queue_data( &p_Device->c_RecvBuffer, recvData,&u_RecvLen,&u_st ) ){
			
	
		if( p_Device->d_Recv != NULL ){
			
			if( p_Device->d_Recv( p_Device, recvData, u_RecvLen ) ){
				
				if( p_Device->d_CreateTimer != NULL ){

					p_Device->d_CreateTimer( &t_timer, recvData, u_RecvLen );
                    
                    if( (t_timer.tr_mode ) ){
                      
                      bsp_findDeltimer( &t_timer );
                    }

				}
				
			}
		}
	}
    
    return TRUE;
	
}

/*****************************************************                       
*				app_Device_SendHandle
* Description:	
* Arguments  :   
* Returns    : 
******************************************************/
void * app_Device_SendHandle( p_AppDevice p_Device ){
	
	unsigned char sendData[DEVICE_RECV_MAX];
	uint16_t      u_SendLen;
    
    unsigned char u_st = 0;
    
    bsp_timer t_timer ;

	memset( &t_timer,0,sizeof(bsp_timer));
	
	p_bsp_timer p_timer = NULL;
	
	if( p_Device == NULL ){
		
		return FALSE;
	}
	
	if( bsp_pop_cir_queue_data( &p_Device->c_SendBuffer, sendData,&u_SendLen, &u_st) ){
		
		
		if( p_Device->d_SendFuc != NULL ){

			p_Device->d_SendFuc( sendData, u_SendLen );

			if( p_Device->d_CreateTimer != NULL ){	
				
                if( p_Device->d_CreateTimer(&t_timer,sendData,u_SendLen)){
                    
                    p_timer = bsp_get_timer( e_tr_one, t_timer.tr_data, 
								   t_timer.u_timer_max,p_Device->d_DealTimerout );
                       
                    if( p_timer != NULL ){
                        
                        memcpy( p_timer->tr_data, t_timer.tr_data, TR_DATA_LEN + sizeof(unsigned short) );
                        p_timer->tr_resv = u_st;
                    }
				}

			}
			
		}
	}
	return p_timer;
}
